// components/count-down/index.js
Component({
  // 外部样式类
  externalClasses: ['custom-class'],
  properties: {
    time: {
      type: Number,
      value: 60
    },
    useSlot: Boolean
  },

  lifetimes: {
    attached() {
      // 启用倒计时
      this.countStart()
    }
  },
  methods: {
    countStart() {
      let timer = null
      // 停止定时器
      if (this.data.time === 0) return clearTimeout(timer)
      // 更新渲染
      this.setData({
        time: (this.data.time -= 1),
      })
      // 定时器
      timer = setTimeout(this.countStart.bind(this), 1000)

      // 触发自定义事件，并传递时间值
      this.triggerEvent('change', this.data.time)
    },
  }
})
